{include file="admin@public/header"/}

<link rel="stylesheet" href="/public/static/admin/easyui/layui-v2.6.8/css/layui.css">
<style type="text/css">
	.layui-form-checkbox[lay-skin=primary] i {
		padding: 0;
	}

	.layui-form-checked[lay-skin=primary] i {
		border-color: #4dc2e6;
		background-color: #4dc2e6;
		color: #fff;
		padding: 0;
	}

	.layui-form-checkbox[lay-skin=primary]:hover i {
		border-color: #4dc2e6;
	}

	#matchCon {
		width: 200px;
	}

	#print {
		margin-left: 10px;
	}

	#saleOrderNumber {
		width: 200px;
	}

	.ul-inline input {
		vertical-align: middle;
	}

	.url-box {
		padding: 5px 15px;
		line-height: 26px;
		cursor: pointer;
	}

	.url-box a {
		margin-right: 5px;
	}

	.btn_query_inventory {
		background: url(/public/static/admin/css/img/btn_query_inventory.png) center center no-repeat;
		height: 100%;
		width: 100%;
		display: block;
	}

	.btn_query_inventory:hover {
		background-image: url(/public/static/admin/css/img/btn_query_inventory_hover.png);
	}

	span.purBillNoLink.link {
		text-decoration: underline;
		cursor: pointer;
	}

	.ui-jqgrid-bdiv .ui-state-highlight,
	.ui-jqgrid-bdiv .ui-state-hover {
		background: none;
		border: none;
	}

	.ui-jqgrid-bdiv .ui-state-distbill {
		background: #fbffc7;
		border: none;
	}

	.islock {
		background: url('/css/img/icon_lock.png')no-repeat;
		display: inline-block;
		width: 12px;
		height: 12px;
		vertical-align: middle;
	}
	.ui-jqgrid-bdiv .jqgrow[aria-selected='false']{
		background:none;
	}
	.ui-jqgrid-bdiv .jqgrow.ui-priority-secondary[aria-selected='false']{
		background: #f8f8f8;
	}
	.ui-jqgrid-bdiv .jqgrow[aria-selected='true']{
		background: #f8ff94;
	}
</style>

<div class="bill-ser-top">
	<ul class="ul-inline cf layui-form">
		<li>
			<input type="text" id="keyword" class="ui-input ui-input-ph" placeholder="请输入单据号或供应商或备注"
				style="width: 190px;font-size: 12px;">
		</li>
		<li>
			<label>交货日期:</label>
			<input type="date" id="startdate" class="ui-input " value="" style="width: 125px;">
			<i>-</i>
			<input type="date" id="enddate" class="ui-input " value="" style="width: 125px;">
		</li>
		<li>
			<input type="text" id="sale_sn" class="ui-input ui-input-ph" placeholder="请输入销售订单号"
				style="width: 190px;font-size: 12px;">
		</li>
		<li>
			<label>销售订单范围:</label>
			<input type="checkbox" id="hasPurReady" name="" lay-skin="primary" title="已采购完订单不显示" checked="">
			<a class="ui-btn ui-btn-search" id="search">查询</a>
		</li>
		<!-- <li class="url-box" id="openUrl"><a href="https://club.kingdee.com/club/newclub/school/course?product_id=7&amp;cid=1548#pid=5704" target="_blank" class="video-icon"></a>购货订单查询</li> -->
	</ul>
</div>


<div class="wrapper btc">
	<div class="bill-ser-botm">
		<div class="cf">
			<div class="ui-config-box">
				<!-- <span  id="config" class="ui-config"><a href="#" class="ui-icon-config-new"></a>列设置</span> -->
			</div>
			<div class="fr">
				<a class="ui-btn-bill" id="toPo" onclick="addFun()">生成购货订单</a>
			</div>
		</div>
	</div>
	<div class="grid-wrap">
		<table id="grid">
		</table>
		<div id="page"></div>
	</div>
</div>





<script type="text/javascript">
	//$(function(){
	$(document).ready(function () {
		var selectIdList=[];
		// 获取当前日期
		var myDate = new Date;
		var year = myDate.getFullYear(); //获取当前年
		var mon = myDate.getMonth() + 1; //获取当前月
		var date = myDate.getDate(); //获取当前日
		// console.log(year + '-' + mon + '-' + date)
		if (mon < 10) {
			mon = "0" + mon;
		}
		if (date < 10) {
			date = "0" + date;
		}
		$("#startdate").val(year + '-' + mon + '-01');
		//$("#startdate").val("2019-10-19");
		$("#enddate").val(year + '-' + mon + '-' + date);


		// 点击查询按钮
		$("#search").on("click", function () {
			//alert("22");
			// 判断（已采购完订单不显示）
			var show_end = 1;  //默认选中
			if ($("#hasPurReady").get(0).checked) {
				show_end = 1;
			} else {
				show_end = 0;
			}
			var startdate = $('#startdate').val();
			var enddate = $('#enddate').val();
			var keyword = $('#keyword').val();//关键字
			var sale_sn = $('#sale_sn').val();//销售单号

			//alert(show_end);
			//$("div.grid-subtitle").text("日期: " + startdate + " 至 " + enddate);
			var param = { 'ajax': '100', 'startdate': startdate, 'enddate': enddate, 'keyword': keyword, 'sale_sn': sale_sn, 'show_end': show_end };  //发送数据
			$("#grid").editCell(1, 19, false);
			//此处可以添加对查询数据的合法验证 
			$("#grid").jqGrid('setGridParam', {
				datatype: 'json',
				postData: param, //发送数据 
				page: 1
			}).trigger("reloadGrid"); //重新载入 


		});


		//alert("1111");
		$("#grid").jqGrid({
			url: '/scm/purchase/index.html?ajax=100',
			datatype: "json",//数据来源，本地数据（local，json,jsonp,xml等）
			height: '90%',//高度，表格高度。可为数值、百分比或'auto'
			//width: Public.setGrid().w,
			//height: Public.setGrid().h,
			colNames: ["商品", "属性", "单位", "数量", "仓库", "客户名称", "客户编号", "订单号", "订单日期", "库存", "购货/销货日期", "购货订单号", "以销定购数量", "实际入库数量", "销货单号", "销货出库数量", "未出库数量", "待采购数量", "<span class='red'>*</span>本次采购数量", "本次采购价格", 'good_id', '销货订单id'],
			colModel: [
				{ name: 'good_name', width: 200 },
				{ name: 'good_model', width: 100 },
				{ name: 'base_unit_name', width: 60 },
				{ name: 'shuliang', width: 60 },
				{ name: 'store_name', width: 100 },
				{ name: 'c_name', width: 100 },
				{ name: 'c_sn', width: 100 },
				{ name: 'sn', width: 150 },
				{ name: 'dj_time', width: 100 },
				{ name: 'use_kc', width: 60, formatter: inventory, },
				{ name: 'gl_dj_time', width: 100 },
				{ name: 'gl_gh_sn', width: 120 },
				{ name: 'gl_gh_shuliang', width: 80 },
				{ name: 'gl_gh_real_shuliang', width: 100 },
				{ name: 'gl_sn', width: 120 },
				{ name: 'gl_shuliang', width: 100 },
				{ name: 'gl_not_shuliang', width: 80 },
				{ name: 'now_shuliang', width: 80 },
				{ name: 'cg_shuliang', width: 85, editable: true },
				{ name: 'g_cg_price', width: 100, editable: true },
				{ name: 'good_id', width: 100, hidden: true },
				{ name: 'xxdd_id', width: 100, hidden: true },
			],
			cellEdit: true,
			cellurl: '/',
			rowNum: 10,
			rowList: [10, 20, 30],
			sortname: 'id',
			sortorder: "desc",
			pagerpos: "left",//分页位置
			multiselect: true,  //自带复选框
			pgbuttons: true,//翻页按钮
			pagination: true,
			altRows: !0,
			gridview: !0,
			shrinkToFit: !1,
			cellLayout: 8,
			autowidth: !0,
			pager: "#page",
			viewrecords: !0,
			cmTemplate: {
				sortable: !1,
				title: !1
			},
			loadError: function (t, e, i) {
				parent.Public.tips({
					type: 1,
					content: "操作失败了哦，请检查您的网络链接！"
				})
			},
			gridComplete: function () {
				$("#grid").editCell(1, 19, true);
			},
			onCellSelect: function (rowid, iCol, cellcontent, e) {
				// console.log(rowid);
				var checkedState = $("#grid #jqg_grid_" + rowid).prop("checked");  //获取当前点击行是否选中

				// $("#grid").setSelection(rowid);  //设置复选框选中
				if(iCol==0){
					
				}else{
					// // 判断复选框选中样式
					if (checkedState) {
						$("#grid").jqGrid("setSelection", rowid, false);
						$("#grid #jqg_grid_" + rowid).prop("checked", false);
						// $("#grid #" + rowid).css({ "background": "#fff" });
					} else {
						$("#grid").jqGrid("setSelection", rowid, true);
						$("#grid #jqg_grid_" + rowid).prop("checked", true);
						// $("#grid #" + rowid).css({ "background": "#f8ff94" });
					}
				}
			}


		});

		$("#grid").jqGrid('setGroupHeaders', {
			useColSpanStyle: true,
			groupHeaders: [
				{ startColumnName: 'good_name', numberOfColumns: 10, titleText: '销货订单信息' },
				{ startColumnName: 'gl_dj_time', numberOfColumns: 8, titleText: '实际出入库数量' },
				{ startColumnName: 'cg_shuliang', numberOfColumns: 3, titleText: '本次采购信息' }
			]
		});
	});


	function inventory(cellvalue, options, rowObject) {
		console.log(rowObject);
		return '<a class="ui-icon-cart btn_query_inventory" data-vname="1002 15年_500ml*6" onclick="storeFun(\'' + rowObject.good_name + '\',\''+rowObject.good_id+'\',\''+rowObject.unit_id+'\')"></a>';
	}

	// 仓库
	function storeFun(good_name,id,unit_id) {
		// $.post("{:url('inventory')}?id="+id);
		layer.open({
			type: 2,
			title: "商品库存查询",
			shadeClose: true,
			shade: 0.3,
			maxmin: true, //开启最大化最小化按钮
			area: ["480px", "500px"],
			btn: ['确认', '关闭'],
			content: "/scm/purchase/inventory.html?id="+id+'&'+'unit_id'+'='+unit_id ,
			btn1: function (index, layero) {
				console.log("确定");
				layer.close(index);
				return false;
			},
			btn2: function (index, layero) {
				layer.close(index);
				return false;
			},
			success: function(layero, index){  
				var body = layer.getChildFrame('body', index);  
				var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();  
				body.find('#matchCon').val(good_name);
			}

		});
	}


	// 生成订单
	function addFun() {
		var ids = $("#grid").jqGrid('getGridParam', 'selarrrow');
		// console.log(ids);
		//var rowData = $('#grid').jqGrid('getRowData',row);
		var selectRowData = [];
		for (var k = 0; k < ids.length; k++) {
			var rowData = $("#grid").jqGrid('getRowData', ids[k]);
			selectRowData.push(rowData);
		}

		if (0 == selectRowData.length) {
			layer.msg("请先选择单据！");
			return false;
		}



		var flag = 0;
		var cgFlag = 0;
		var indexArr = [];
		//console.log(selectRowData);
		selectRowData.forEach(function (self, index) {

			// 判断待采购数量存在且大于0
			if (self['now_shuliang'] != null && self['now_shuliang'] != "" && self['now_shuliang'] > 0) {
				cgFlag = cgFlag + 1;
			}

			// 判断 本次采购数量 存在
			if (self['cg_shuliang'] != null && self['cg_shuliang'] != "") {
				flag = flag + 1;
			} else {
				indexArr.push(index);
			}


		});

		if (cgFlag != selectRowData.length) {
			layer.msg("采购数量需大于0，否则无法生成购货订单！");
			return false;
		}

		if (flag != selectRowData.length) {
			layer.msg("请录入本次采购数量！");
			$("#grid").editCell(indexArr[0] + 1, 19, true);
			return false;
		}

		var selectRow = JSON.stringify(selectRowData);
		parent.tab.addTabItem({
			tabid: "purchaseinvSa",
			text: "购货订单",
			url: "/scm/purchase/edit.html?xxdd_ids=" + ids + "&xxdd_RowData=" + selectRow,
		})
		console.log(ids);
	}

</script>

{include file="admin@public/footer"/}